package com.sevice;

import java.util.*;

/**
 * 彩票
 *
 * @author yuanqinglong
 * @since 2021/12/17 10:04
 */
public abstract class AbstractLottery {

    static Set<Integer> lotteryTypes = new HashSet<>();

    static {
        lotteryTypes.add(1);
        lotteryTypes.add(2);
    }

    protected int numberOfRed;
    protected int numberOfBlue;
    protected int votes;
    protected int multiple;

    /**
     * 检查红色的数量
     *
     * @param redAmount 红色的数量
     * @return int
     */
    abstract int checkRedAmount(int redAmount);

    /**
     * 检查蓝色的数量
     *
     * @param blueAmount 蓝色的数量
     * @return int
     */
    abstract int checkBlueAmount(int blueAmount);

    /**
     * 得到红球列表
     *
     * @return {@link List}<{@link String}>
     */
    abstract List<String> getRedBallList();

    /**
     * 得到蓝色球列表
     *
     * @return {@link List}<{@link String}>
     */
    abstract List<String> getBlueBallList();


    /**
     * 押注
     *
     * @param scanner   扫描仪
     * @param howToPlay 怎么玩
     */
    abstract void betting(Scanner scanner, int howToPlay);

    /**
     * 消费
     */
    abstract void consumption();


    public static void main(String[] args) {
        Map<String, Integer> countRed = new HashMap<>(8);
        Map<String, Integer> countBlue = new HashMap<>(8);
        Scanner scanner = new Scanner(System.in);
        System.out.print("1 买双色球、2 买大乐透：");
        int type = scanner.nextInt();
        System.out.print("1 单式、2 复式：");
        int howToPlay = scanner.nextInt();
        AbstractLottery lottery = lotteryTypes.contains(type) ? (type == 1 ? new TwoColorBall() : new Lotto()) : new Lotto();
        lottery.betting(scanner, howToPlay);
        for (int i = 0; i < lottery.votes; i++) {
            lottery.getRedBallList().stream().limit(lottery.numberOfRed).sorted().forEach(red -> {
                countRed.merge(red, 1, Integer::sum);
                System.out.print("\033[1;31m" + red + " ");
            });
            System.out.print("- ");
            lottery.getBlueBallList().stream().limit(lottery.numberOfBlue).sorted().forEach(blue -> {
                countBlue.merge(blue, 1, Integer::sum);
                System.out.print("\033[1;34m" + blue + " ");
            });
            System.out.print("\033[1;34m (" + lottery.multiple + ")");
            System.out.println();
        }
        System.out.println();
        countRed.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(lottery.numberOfRed).sorted(Map.Entry.comparingByKey()).forEach(integerIntegerEntry -> System.out.print("\033[1;31m" + integerIntegerEntry.getKey() + " "));
        System.out.print("- ");
        countBlue.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder())).limit(lottery.numberOfBlue).sorted(Map.Entry.comparingByKey()).forEach(integerIntegerEntry -> System.out.print("\033[1;34m" + integerIntegerEntry.getKey() + " "));
        System.out.print("(hot)");
        System.out.println();
        System.out.println();
        lottery.consumption();

    }


    public static int A(int bellow, int up) {
        int result = 1;
        for (int i = up; i > 0; i--) {
            result *= bellow;
            bellow--;
        }
        return result;

    }

    /**
     * 实现组合，需用到排列的方法。
     */
    public static int C(int below, int up) {
        int denominator = A(up, up);
        int numerator = A(below, up);
        return numerator / denominator;
    }


}
